﻿#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Xlsx表查询数据
/// </summary>
class EditorWindowSFXLSXConfigQueryData : AbsESF_EditorWindow
{
    /// <summary>
    /// 节点
    /// </summary>
    ESF_CollectAsset_XLSX mNode = null;
    /// <summary>
    /// 主键值
    /// Key:主键名称
    /// Value:主键值
    /// </summary>
    Dictionary<string, object> mPKValues = new Dictionary<string, object>();
    /// <summary>
    /// 主键列
    /// Key:主键名称
    /// Value:主键值
    /// </summary>
    Dictionary<string, ESF_Xml_XlsxTableColumn_ScriptBuildSetting> mPKColumns = new Dictionary<string, ESF_Xml_XlsxTableColumn_ScriptBuildSetting>();
    /// <summary>
    /// 当前选择语言
    /// </summary>
    enESF_LanguageClassify mSelectLanguage = enESF_LanguageClassify.CN;
    /// <summary>
    /// 查询表数据
    /// </summary>
    XlsxDbTable mQueryTable = null;
    /// <summary>
    /// 查询行的数据
    /// </summary>
    XlsxDbTableRow mQueryTableRow = null;
    /// <summary>
    /// 输入查询行的数据
    /// </summary>
    int mQueryInputTableRowOrdinal = 0;
    /// <summary>
    /// 显示查询行的数据
    /// </summary>
    int mQueryShowTableRowOrdinal = 0;
    /// <summary>
    /// 滚动视图位置
    /// </summary>
    Vector2 mScrollViewPosition = Vector2.zero;
    /// <summary>
    /// 结果提示
    /// </summary>
    string mResultTip = string.Empty;
    /// <summary>
    /// 搜索列
    /// </summary>
    List<string> mSearchColumn = new List<string>();

    protected override AbsESF_EditorBuildAsset config => null;

    /// <summary>
    /// 设置数据
    /// </summary>
    /// <param name="_node">节</param>
    public void SetData(ESF_CollectAsset_XLSX _node)
    {
        mNode = _node;
        if (mNode != null)
        {
            if (mNode.xlsxScriptBuildSettingConfig.xml.pkColumns != null)
            {
                foreach (var key in mNode.xlsxScriptBuildSettingConfig.xml.pkColumns)
                {
                    mPKValues.Add(key.csName, null);
                    mPKColumns.Add(key.csName, key);
                }
            }
            mNode.AutoCreateTableAsset();            
        }
    }

    protected override void OnRunGUI()
    {
        if (mNode != null)
        {
            OnDrawBrower();
            OnDrawPk();
        }
    }

    /// <summary>
    /// 绘制Brower
    /// </summary>
    private void OnDrawBrower()
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField($"XLSX表：{mNode.path}");
        if (GUILayout.Button($"{ ESF_enGUIDisplayWord.Brower.ESF_ToAlias()}"))
        {
            ESF_Utility.PingObject(mNode.path);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField($"XLSX配置：{mNode.xlsxTableSchemaXmlConfig.filePath}");
        if (GUILayout.Button($"{ ESF_enGUIDisplayWord.Brower.ESF_ToAlias()}"))
        {
            ESF_Utility.PingObject(mNode.xlsxTableSchemaXmlConfig.filePath);
        }
        EditorGUILayout.EndHorizontal();
        EditorGUILayout.BeginHorizontal();
        mSelectLanguage = (enESF_LanguageClassify)ESF_Utility.EnumPopup(mSelectLanguage, "语言类别：");
        int lang = (int)mSelectLanguage;
        if (mNode.dbTables.ContainsKey(lang))
        {
            mQueryTable = mNode.dbTables[lang];            
        }
        if (GUILayout.Button($"{ ESF_enGUIDisplayWord.Brower.ESF_ToAlias()}{mQueryTable.name}"))
        {
            ESF_Utility.PingObject(mQueryTable);
        }
        EditorGUILayout.EndHorizontal();
    }

    /// <summary>
    /// 绘制主键
    /// </summary>
    private void OnDrawPk()
    {
        if (mPKColumns != null && mPKColumns.Count > 0)
        {
            foreach (KeyValuePair<string, ESF_Xml_XlsxTableColumn_ScriptBuildSetting> pk in mPKColumns)
            {
                if (mPKValues[pk.Key] != null)
                {
                    mPKValues[pk.Key] = EditorGUILayout.TextField($"{pk.Value.csName}：", mPKValues[pk.Key].ToString());
                }
                else
                {
                    mPKValues[pk.Key] = EditorGUILayout.TextField($"{pk.Value.csName}：", string.Empty);
                }
            }
        }
        else
        {
            string text = EditorGUILayout.TextField($"Row：", mQueryInputTableRowOrdinal.ToString());
            int.TryParse(text, out mQueryInputTableRowOrdinal);
        }
        if (GUILayout.Button($"Query"))
        {
            OnQueryRow();
        }
        OnShowTableRow();
    }

    /// <summary>
    /// 查询数据
    /// </summary>
    void OnQueryRow()
    {
        mQueryTableRow = null;
        mResultTip = string.Empty;
        mSearchColumn.Clear();
        if (mQueryTable != null && mQueryTable.rows != null)
        {
            if (mPKColumns != null && mPKColumns.Count > 0)
            {
                bool isFindRow = false;
                mQueryInputTableRowOrdinal = 0;
                foreach (XlsxDbTableRow row in mQueryTable.rows)
                {
                    isFindRow = true;
                    foreach (KeyValuePair<string, ESF_Xml_XlsxTableColumn_ScriptBuildSetting> pk in mPKColumns)
                    {
                        isFindRow &= mNode.EqualsValue(row, pk.Value, mPKValues[pk.Key]);                        
                    }
                    if (isFindRow)
                    {
                        mQueryTableRow = row;
                        mQueryShowTableRowOrdinal = mQueryInputTableRowOrdinal;
                        break;
                    }
                    mQueryInputTableRowOrdinal++;
                }
            }
            else if (mQueryInputTableRowOrdinal >= 0)
            {
                mQueryInputTableRowOrdinal = Mathf.Clamp(mQueryInputTableRowOrdinal, 0, mQueryTable.rows.Length - 1);
                mQueryShowTableRowOrdinal = mQueryInputTableRowOrdinal;
                mQueryTableRow = mQueryTable.rows[mQueryShowTableRowOrdinal];
            }
        }
        if (mQueryTableRow == null)
        {
            foreach (var key in mPKValues)
            {
                mSearchColumn.Add($"[{key.Key}={key.Value}]");
            }            
            mResultTip = $"Can't not find {mSearchColumn.ESF_Join(enESF_SeparatorSymbol.Comma)}";            
        }
    }

    /// <summary>
    /// 显示行数据
    /// </summary>
    void OnShowTableRow()
    {
        mScrollViewPosition = EditorGUILayout.BeginScrollView(mScrollViewPosition);
        if (mQueryTableRow != null)
        {
            bool isDet = mNode.xlsxTableSchemaXmlConfig.xml.xlsxTableClassify == ESF_enXlsxTableClassify.Determinant;
            string cellString = string.Empty;
            foreach (var key in mNode.xlsxScriptBuildSettingConfig.xml.columns)
            {
                if (isDet)
                {
                    cellString = $"Cell【{ mQueryTableRow.xlsxRowIndex},{ key.xlsxOrdinal.XlsxColumnOrdinalToLetter()}_{ key.xlsxOrdinal}】";
                }
                else
                {
                    cellString = $"Cell【{mQueryTableRow.xlsxRowIndex},{ key.xlsxOrdinal.XlsxColumnOrdinalToLetter()}_{ key.xlsxOrdinal}】";
                }
                EditorGUILayout.LabelField($"{cellString} {key.csName}=>{mNode.ResolverColumnValue(key, mQueryTableRow)}");
            }
        }
        else if (!string.IsNullOrEmpty(mResultTip))
        {
            EditorGUILayout.HelpBox(mResultTip, MessageType.Info);
        }
        EditorGUILayout.EndScrollView();
    }
}
#endif
